//
//  RHDataStatusView.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/7/10.
//

#import "RHDataStatusView.h"
#import "RHDeviceStatusCircleView.h"

@interface RHDataStatusView()
@property (nonatomic, strong) RHDeviceStatusCircleView *circleView;
@end
@implementation RHDataStatusView

-(instancetype)init
{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}

- (void)setDic:(NSDictionary *)dic
{
    _dic = dic;

    NSInteger normalCount  = [ValidNullData(dic[@"online"]) integerValue];
    NSInteger offlineCount = [ValidNullData(dic[@"offline"]) integerValue];
    NSInteger faultCount   = [ValidNullData(dic[@"faultNumber"]) integerValue];
    NSInteger alarmCount   = [ValidNullData(dic[@"alarmNumber"]) integerValue];
    NSInteger totalCount   = normalCount + offlineCount + faultCount + alarmCount;
    CGFloat normalPercent  = normalCount * 1.0 / totalCount;
    CGFloat offlinePercent = offlineCount * 1.0 / totalCount;
    CGFloat faultPercent   = faultCount * 1.0 / totalCount;
    CGFloat alarmPercent   = alarmCount * 1.0 / totalCount;
    if(totalCount == 0){
         normalPercent  = 0;
         offlinePercent = 0;
         faultPercent   = 0;
         alarmPercent   = 0;
    }
    
    NSArray *percentArr = @[@(normalPercent),@(alarmPercent),@(faultPercent),@(offlinePercent)];
    NSArray *countArr = @[@(normalCount),@(alarmCount),@(faultCount),@(offlineCount)];
    [percentArr enumerateObjectsUsingBlock:^(NSNumber*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RHDeviceStatusCircleView *circleView = [self viewWithTag:100+idx];
        circleView.percent = [obj floatValue];
        circleView.startAngle = [self.angleArr[idx] integerValue]*(M_PI/180.0);
        QMUILabel *countLab = [self viewWithTag:200+idx];
        countLab.text = [NSString stringWithFormat:@"%zd",[countArr[idx] integerValue]];
        
        QMUILabel *percentLab = [self viewWithTag:300+idx];
        percentLab.text = [NSString stringWithFormat:@"%@%%",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:1 value :[obj doubleValue]*100]];
    }];
}


-(void)setupUI
{
    kWeakSelf;
    
    
    //-------------
    
    NSArray *titleArr = @[@"正常".rh_localized,@"告警".rh_localized,@"故障".rh_localized,@"离线".rh_localized];
    NSArray *colorArr = @[@"9FE7B8",@"F790A1",@"FEDB5B",@"BBBBBB"];
   
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = @"设备状态".rh_localized;
    titleLab.textColor = kCOLOR_HEX(@"#171A1D");
    titleLab.textAlignment = NSTextAlignmentRight;
    titleLab.font = kBOLDSYSTEMFONT(16);
    [self addSubview:titleLab];
 
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.16);
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(titleLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    
    UIView *bgView = kView;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.top.equalTo(line.mas_bottom).offset(1);
        make.right.mas_offset(0);
        make.bottom.mas_offset(-5);
    }];
    
    
   
    CGFloat circleSize = kScaleW(45);
    CGFloat itemWidth = (kUI_WINDOW_W - 30)/4;
    NSLog(@"---->itemWidth:%f",itemWidth);
    [titleArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemBgView = kView;
        [bgView addSubview:itemBgView];
        
        [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(0);
            make.left.mas_offset(itemWidth*idx);
            make.width.offset(itemWidth);
            make.bottom.mas_offset(0);
        }];
        RHDeviceStatusCircleView *circleview = [[RHDeviceStatusCircleView alloc]init];
        circleview.tag = 100+idx;
        circleview.lineColor = colorArr[idx];
        [itemBgView addSubview:circleview];
        [circleview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(10);
            make.centerX.mas_offset(0);
            make.width.height.offset(circleSize);
        }];
        
        //百分比
        QMUILabel *percentLab = [[QMUILabel alloc]init];
        percentLab.tag = 300 + idx;
        percentLab.text = @"(--%)";
        percentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
        percentLab.textAlignment = NSTextAlignmentLeft;
        percentLab.font = [UIFont boldSystemFontOfSize:12];
        [itemBgView addSubview:percentLab];
       
        [percentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(circleview);
        }];
        
        //数量
        QMUILabel *contentLab = [[QMUILabel alloc]init];
        contentLab.tag = 200 + idx;
        contentLab.text = @"--";
        contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
        contentLab.textAlignment = NSTextAlignmentRight;
        contentLab.font = [UIFont boldSystemFontOfSize:15];
        [itemBgView addSubview:contentLab];
       
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(circleview.mas_bottom).offset(10);
            make.centerX.equalTo(circleview);
        }];
        
        
       
        //分类
        QMUILabel *titleLab = [[QMUILabel alloc]init];
        titleLab.numberOfLines = 0;
        titleLab.tag = 300 + idx;
        titleLab.text = titleArr[idx];
        titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
        titleLab.textAlignment = NSTextAlignmentLeft;
        titleLab.font = [UIFont systemFontOfSize:13];
        [itemBgView addSubview:titleLab];
      
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_offset(0);
            make.top.equalTo(contentLab.mas_bottom).offset(10);
        }];
        
       
    }];
    
    
    
   
}

@end
